package info.thereisonlywe.onenesspathfinder.controllers;

import info.thereisonlywe.core.logging.LiteLogger;
import info.thereisonlywe.core.toolkit.ThreadToolkit;
import info.thereisonlywe.onenesspathfinder.ui.Tray;
import info.thereisonlywe.onenesspathfinder.ui.UIDelegate;

import java.io.File;
import java.util.logging.Level;

public class MasterController {

	public static void exit(int code)
	{
		if (code == 0)
		{
			UIDelegate.setFrameVisibility(false);
			RecitationController.getImam().terminate();
			RecitationController.getImam().terminate();
			UIDelegate.saveSettings();
			UIDelegate.BookmarkPaneHook.getBookmarkList().saveBookmarks(
					new File("userdata" + File.separator + "bookmarks.qbm"));
			ThreadToolkit.sleep(500);
			if (Tray.getSystemTray().getTrayIcons().length > 0)
				Tray.getSystemTray().remove(
						Tray.getSystemTray().getTrayIcons()[0]);
			UIDelegate.disposeFrame();
			LiteLogger.log(Level.FINER, "Exited normally.");
			LiteLogger.flush();
		}

		else if (code == 1)
		{
			LiteLogger.log(Level.SEVERE, "UI initialization failed!");
		}

		else if (code == 2)
		{
			LiteLogger.log(Level.SEVERE, "A critical file is missing!");
		}

		System.exit(code);
	}

}
